const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    brand_list: [],
    keyword: '',
    info: null,
    pics: [],
    searchShow: false
  },
  backto: function (e) {
    wx.navigateBack({})
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    if(options.type == 'edit'){
      // wx.setNavigationBarTitle({
      //   title: '编辑车型报价',
      // })
      app.ihttpGet('https://www.58zltc.com/api.php/Goods/item/id/' + options.id, res => {
        this.setData({
          type: 'edit',
          info: res.data,
          pics: res.data.pics
        })
      })
    }else if(!options.car_id){
      // wx.setNavigationBarTitle({
      //   title: '添加车型报价',
      // })
      this.setData({
        searchShow: true,
        step: options.step
      })
      switch(options.step){
        case '1':
          app.ihttpGet('https://www.58zltc.com/api.php/Car/brandList/openid/' + app.globalData.openid, res => {
            this.setData({
              list: res.data
            })
          })
          break;
        case '2':
          app.ihttpGet('https://www.58zltc.com/api.php/CarSpider/get_sub_brand/id/' + options.id + '/carid/' + options.carid, res => {
            console.log('++++++++++++++0-2')
            console.log(res.data)
            this.setData({ 
              list: res.data
            })
          })
          break;
        case '3':
          app.ihttpGet('https://www.58zltc.com/api.php/CarSpider/get_car_list/id/' + options.id + '/carid/' + options.carid, res => {
            console.log('++++++++++++++0-3')
            console.log(res.data)
            this.setData({
              list: res.data
            })
          })
          break;
      }
    }else{
      wx.setNavigationBarTitle({
        title: '添加车型报价',
      })
      this.setData({
        info: options
      })
    }
  },
  selectBrand: function (e) {
    const step = this.data.step;
    var data = e.currentTarget.dataset;
    switch (step){
      case '1':
        console.log('+++++++++++++++++++++++1')
        console.log(data)
        wx.redirectTo({
          url: 'add?step=2&id=' + data.brandid + '&carid=' + data.id,
        })
        break;
      case '2':
        console.log('+++++++++++++++++++++++2')
        console.log(data)
        wx.redirectTo({
          url: 'add?step=3&id=' + data.id + '&carid=' + data.carid,
        })
        break;
      case '3':
        console.log('+++++++++++++++++++++++3')
        console.log(data)
        wx.redirectTo({
          url: 'add?car_id=' + data.id + '&title=' + data.name + '&carid=' + data.carid,
        })
        break;
    }
    // var data = e.currentTarget.dataset;
    // var name = this.data.keyword+" "+data.name;
    // wx.redirectTo({
    //   url: 'add?title='+name+'&car_id='+data.id,
    // })
  },
  bindUploadImg: function () {
    var that = this;
    let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
    const token = extConfig.appid;
    wx.chooseImage({
      count: 1,
      success: function (res) {
        var tempFilePaths = res.tempFilePaths
        wx.uploadFile({
          url: 'https://www.58zltc.com/api.php/Index/uploadImage?token=' + token,
          filePath: tempFilePaths[0],
          name: 'file',
          success: function (res) {
            var data = JSON.parse(res.data);
            console.log(data);
            if (data.err_code > 0) {

            } else {
              var pics = that.data.pics;
              var file_name = data.data.domain;
              console.log(file_name);
              pics.push(file_name);
              that.setData({
                pics: pics
              })
            }
            //do something
          }
        })
      }
    })
  },
  formSubmit: function (e) {
    // console.log(e.detail.value);
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    var post_data = e.detail.value;
    var data = this.data;
    if (!data.pics) {
      wx.showToast({
        title: '请选择汽车图片！',
      })
      return;
    }
    post_data.openid = app.globalData.openid;
    post_data.pics = data.pics;
    post_data.img_url = data.pics[0];
    post_data.car_id = data.info.car_id;
    post_data.car_brand_id = data.info.carid;
    if (data.type == 'edit') {
      post_data.id = data.info.id;
    }

    app.ihttpPost('https://www.58zltc.com/api.php/Car/carAdd', post_data,  res => {
      if (res.data.err_code > 0) {
        wx.showToast({
          title: res.data.err_msg,
        })
      } else {
        wx.showToast({
          title: res.data.err_msg,
          icon: 'success'
        })
        setTimeout(function () {
          wx.navigateBack();
        }, 500)
      }
    })
  }
})